from django.shortcuts import render
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination

from .models import SKU
from .serializers import SKUListSerializer, SKUSearchSerializer

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUSearchSerializer


class SKUListView(ListAPIView):
    """商品列表"""

    # 指定过滤后端为排序
    filter_backends = [OrderingFilter]
    # 指定以那几个字段进行排序
    ordering_fields = ('create_time', 'price', 'sales')
    # 只对当前视图进行分页
    # pagination_class = StandardResultsSetPagination
    # 指定序列化器
    serializer_class = SKUListSerializer


    # 指定查询集
    # queryset = SKU.objects.filter(is_launched=True, category_id=xxx)
    def get_queryset(self):
        # 视图对象.kwargs属性可以拿到正则组别名的参数数据
        # 视图对象.args属性可以拿到正则组没有别名的参数数据
        cat_id = self.kwargs.get('category_id')
        return SKU.objects.filter(is_launched=True, category_id=cat_id)


